package com.gxd.web.servlet;

import com.gxd.domain.Cart;
import com.gxd.domain.Goods;
import com.gxd.domain.User;
import com.gxd.service.CartService;
import com.gxd.service.GoodsService;
import com.gxd.service.GoodsTypeService;
import com.gxd.service.impl.CartServiceImpl;
import com.gxd.service.impl.GoodsServiceImpl;
import com.gxd.util.NotNull;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/cartservlet")
public class CartServlet extends ServletBase {
    CartService cartService = new CartServiceImpl();
    public void clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        cartService.clearCart(user.getId());
        response.getWriter().print("清空购物车成功");
    }

    public void addCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object user1 = request.getSession().getAttribute("user");
        User user= (User) user1;
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findGoodById(Integer.parseInt(goodsId));
        Cart cart = cartService.findUidAndPid(user.getId(), Integer.parseInt(goodsId));
        if(cart==null){
            cart=new Cart(user.getId(),Integer.parseInt(goodsId),Integer.parseInt(number),Integer.parseInt(number)*goods.getPrice());
            cartService.insert(cart);
        }else{
            cart.setNum(cart.getNum()+Integer.parseInt(number));
            if(Integer.parseInt(number)==0){
                cartService.deleteCart(user.getId(),Integer.parseInt(goodsId));
            }else {
                cart.setMoney(cart.getNum()*goods.getPrice());
                cartService.update(cart);
            }
        }
response.getWriter().write("success");
    }
    public String getCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object user1 = request.getSession().getAttribute("user");
        if(user1==null){
            return "redirect:/login.jsp";
        }
        User user = (User) user1;
        List<Cart> carts = cartService.findUid(user.getId());
        request.setAttribute("carts",carts);
        return "forward:/cart.jsp";
    }

    public String addCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object userObj = request.getSession().getAttribute("user");
        if(userObj==null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(NotNull.isEmpty(goodsId)||NotNull.isEmpty(number)){
            request.setAttribute("msg","加入购物车失败");
            return "forward:/message.jsp";
        }
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findGoodById(Integer.parseInt(goodsId));
        User user = (User) userObj;
        Cart cart=cartService.findUidAndPid(user.getId(),Integer.parseInt(goodsId));

        if(cart==null){
            cart=new Cart(user.getId(),Integer.parseInt(goodsId),Integer.parseInt(number),Integer.parseInt(number)*goods.getPrice());
            cartService.insert(cart);
        }else{
            cart.setNum(cart.getNum()+Integer.parseInt(number));
            cart.setMoney(cart.getNum()*goods.getPrice());
            cartService.update(cart);
        }
        return "redirect:/cartSuccess.jsp";
    }
}
